package com.tanhua.service;


import cn.hutool.core.date.DateUtil;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.mapper.AnalysisByDayMapper;
import com.tanhua.mapper.LogMapper;
import com.tanhua.utils.ComputeUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

@Service
public class AnalysisByDayService {
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    @Autowired
    private LogMapper logMapper;
    public ResponseEntity summary() {
        AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();

        // 累计用户数
        Long cumulativeUsers = analysisByDayMapper.queryCumulativeUsers();
        summaryVo.setCumulativeUsers(cumulativeUsers);
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        System.out.println(todayStr);
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");

        //过去30天活跃用户数
        Long activePassMonth = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(30).toString());
        summaryVo.setActivePassMonth(activePassMonth);

        //过去7天活跃用户数
        Long activePassWeek = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(7).toString());
        summaryVo.setActivePassWeek(activePassWeek);

        //今日新增用户数量
        Integer newUsersToday = logMapper.findNumRegistered("0102",todayStr);
        summaryVo.setNewUsersToday(newUsersToday.longValue());
        //昨日新增用户
        Integer newUsersYesToday = logMapper.findNumRegistered("0102",yesterday);
        //环比新增用户
        BigDecimal bigDecimal = ComputeUtil.computeRate(newUsersToday, newUsersYesToday);
        summaryVo.setNewUsersTodayRate(bigDecimal);

        //今日登录次数
        Integer loginTimesToday = logMapper.findNumRegistered("0101",todayStr);
        System.out.println(loginTimesToday);
        summaryVo.setLoginTimesToday(loginTimesToday.longValue());

        // 昨日登录次数
        Integer loginTimesYesToday = logMapper.findNumRegistered("0101",yesterday);
        System.out.println(loginTimesYesToday);
        // 环比登录次数
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, loginTimesYesToday);
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);

        //今日活跃用户数量
        Integer activeUsersToday = logMapper.findActive(todayStr);
        summaryVo.setActiveUsersToday(activeUsersToday.longValue());
        //昨天活跃用户数量
        Integer activeUsersYesToday = logMapper.findActive(yesterday);
        //环比活跃用户
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, activeUsersYesToday);
        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);

        return ResponseEntity.ok(summaryVo);
    }
}
